Game xếp hình 2048

13.193 lượt xem;
1 using UnityEngine;
2 using
System.Collections;
3
4 public
class TimerScript : MonoBehaviour {
5
6     
private GameControllerScript gameScript;
7     
private OptionsScript options;
8     
private GUISkin currentGUISkin;
9     
private bool timerEnabled;
10     
private int timeRemaining;
11     
public AudioClip countdownSound;
12     
private AudioSource countdownAudioSource; //reserved for countdown
13
14     
// Use this for initialization
15     
void Start () {
16         
this.gameScript = this.gameObject.GetComponent ("GameControllerScript") as GameControllerScript;
17         
this.options = this.gameObject.GetComponent ("OptionsScript") as OptionsScript;
18         
this.currentGUISkin = gameScript.currentGUISkin;
19         
this.InitTimer ();
20
21         AudioSource[] audioSources = GetComponents<AudioSource>();
22         
this.countdownAudioSource = audioSources[2];
23         countdownAudioSource.clip =
this.countdownSound;
24     }
25     
26     
// Update is called once per frame
27     
void Update () {
28     
29     }
30
31     
void OnGUI () {
32         
33         GUI.skin =
this.gameScript.currentGUISkin;
34         
if(this.timerEnabled
35            && gameScript.gameView ==
"game"
36         ) {
37             
38             GUI.Label (
new Rect (0, Screen.height * 0.70f , Screen.width * 0.35f, Screen.height * 0.15f),
39                        
this.timeRemaining.ToString(), "BigLabel");
40
41             GUI.Label (
new Rect (Screen.width * 0.0f, Screen.height * 0.77f , Screen.width * 0.35f, Screen.height * 0.08f),
42                        
"Target");
43
44             
45             
int nextHighest = PlayerPrefs.GetInt ("game_highest_block") * 2;
46             
if (nextHighest < 64) nextHighest = 64;
47             GUI.Label (
new Rect (Screen.width * 0.0f, Screen.height * 0.82f , Screen.width * 0.35f, Screen.height * 0.08f),
48                        nextHighest.ToString ());
49         }
50     }
51
52     
//initialize the timer
53     
public void InitTimer() {
54         
//set time remaining if it doesn't exist yet
55         
if (!PlayerPrefs.HasKey ("timer_time_remaining")){
56             
this.timeRemaining = PlayerPrefs.GetInt("options_timer_duration");
57             PlayerPrefs.SetInt (
"timer_time_remaining", this.timeRemaining);
58         }
59         
else {
60             
this.timeRemaining = PlayerPrefs.GetInt ("timer_time_remaining");
61         }
62         PlayerPrefs.Save ();
63
64         
65         CancelInvoke (
"TimeChange");
66         
if (PlayerPrefs.GetInt ("options_timer_duration") > 0) {
67             
this.timerEnabled = true;
68             InvokeRepeating (
"TimeChange", 1, 1);
69         }
70         
else {
71             
this.timerEnabled = false;
72         }
73     }
74
75     
public void ResetTimer() {
76         
this.InitTimer ();
77         
this.timeRemaining = PlayerPrefs.GetInt ("options_timer_duration") + 5;
78         PlayerPrefs.SetInt (
"timer_time_remaining", this.timeRemaining);
79         PlayerPrefs.Save();
80     }
81
82     
private void TimeChange() {
83         
if (this.gameScript.gameView == "game"
84             && PlayerPrefs.GetString (
"game_status") == "playing"
85         ) {
86             
if(
87                 
this.options.play_sounds
88                 && 
this.timeRemaining <= 6
89                 && 
this.timeRemaining > 1
90             ) {
91                 
this.countdownAudioSource.Play();
92             }
93             
if(this.timeRemaining == 1) {
94                 PlayerPrefs.SetString (
"game_status", "game_over");
95             }
96             
this.timeRemaining = this.timeRemaining - 1;
97         }
98
99         PlayerPrefs.SetInt (
"timer_time_remaining", this.timeRemaining);
100         PlayerPrefs.Save ();
101
102     }
103
104     
/**
105      * Set the next block target
106      *
107      */

108     
public void SetNextBlockTarget(int TargetBlock) {
109         
//add the amount of time necessary for the next number to be reached
110         
if(TargetBlock >= 64) {
111             
int addTime = PlayerPrefs.GetInt ("options_timer_duration") * TargetBlock / 64;
112             
this.timeRemaining = this.timeRemaining + addTime;
113         }
114     }
115 }


private AudioSource countdownAudioSource; reserved for countdown

Use this for initialization

Update is called once per frame

initialize the timer

set time remaining if it doesn't exist yet

add the amount of time necessary for the next number to be reached



Gõ tìm kiếm nhanh...